﻿using CoreRp.Db_Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CoreRp.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [Filters.AuthRole]
    public class UserController : ControllerBase
    {
        public UserController(IFreeSql db)
        {
            this.Db = db;
        }

        public IFreeSql Db { get; }
        [HttpGet("page")]

        public Models.M_PageView<Db_Models.RAP_USER> Get([FromQuery] int page, [FromQuery] int limit,[FromQuery]string LOGINNAME,[FromQuery]string REALNAME)
        {
            var pm = new Models.M_PageView<Db_Models.RAP_USER>();
            try
            {
                // demo:多条件Like分页查询
                pm.List = Db.Select<Db_Models.RAP_USER>()
                    .WhereIf(!string.IsNullOrWhiteSpace(LOGINNAME), x => x.LOGINNAME.Contains(LOGINNAME))
                    .WhereIf(!string.IsNullOrWhiteSpace(REALNAME), x => x.REALNAME.Contains(REALNAME))
                    .Count(out long count).Page(page, limit).ToList();

                pm.Total = count;
                return pm;
            }
            catch (Exception ex)
            {
                throw new BadRequestException("请求失败", ex);
            }
        }
        [HttpGet]

        public Db_Models.RAP_USER GetItem([FromQuery]string usercode)
        {

            return Db.Select<RAP_USER>().Where(x => x.USERCODE == usercode && x.COMPANYCODE == Request.GetCompanyCode()).First();
        }
        [HttpPost]
        public RAP_USER Post([FromBody]RAP_USER model)
        {
            if (model != null)
            {
                model.USERCODE = Guid.NewGuid().ToString();
                model.COMPANYCODE = Request.GetCompanyCode();
                model.PASSWORD = model.PASSWORD?.Md5();
                Db.Insert<RAP_USER>(model).ExecuteAffrows();
                model.PASSWORD = "******";
                return model;
            }
            else
            {
                throw new Exception("参数不能为空");
            }
        }
        [HttpPut]
        public RAP_USER Put([FromBody]RAP_USER model)
        {
            if (model != null)
            {
                model.USERCODE = Guid.NewGuid().ToString();
                model.COMPANYCODE = Request.GetCompanyCode();
                model.PASSWORD = model.PASSWORD?.Md5();
                Db.Insert<RAP_USER>(model).ExecuteAffrows();
                model.PASSWORD = "******";
                Db.Update<RAP_USER>().Where(x => x.USERCODE == model.USERCODE && x.COMPANYCODE==model.COMPANYCODE).Set(x => model).ExecuteAffrows();
                return model;
            }
            else
            {
                throw new Exception("参数不能为空");
            }

        }
        [HttpDelete]
        public void Delete([FromQuery]string usercode)
        {
            Db.Delete<RAP_USER>().Where(x => x.USERCODE == usercode && x.USERCODE != "SUPERADMINISNOTAGUID").ExecuteAffrows();
        }
    }
}
